#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int n, m, s;
const int N = 1e4 + 10;
int dis[N];
int map[N][N];
int vis[N];
void Dij(int x)
{
	memset(dis, 0x3f, sizeof(dis));
	dis[x] = 0;
	vis[x] = 1;
	for (int i = 1; i <= n; i++) {
		if (i != x) {
			dis[i] = min(dis[i], map[x][i] + dis[x]);
		}
	}
	while (1) {
		int pos = -1;
		int mi = 0x3f3f3f3f;
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && dis[i] < mi) {
				mi = dis[i];
				pos = i;
			}
		}
		if (pos == -1) {
			break;
		}
		for (int i = 1; i <= n; i++) {
			if (i != pos){
				dis[i] = min(dis[i], dis[pos] + map[pos][i]);
			}
		}
		vis[pos] = 1;
	}
}
int main()
{
	cin >> n >> m >> s;
	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		map[a][b] = c;
	}
	Dij(s);
	for (int i = 1; i <= n; i++) {
		cout << dis[i] << " ";
	}
	return 0;
}